import { Component, ViewChild} from '@angular/core';
import { Content } from 'ionic-angular';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ListService } from './services/list.service'
import * as $ from 'jquery';
import * as moment from 'moment'
import { Utils } from './Utils';
import { DetailsService } from '../details/services/details.service';
import { ChangeDetectorRef } from '@angular/core';



@IonicPage()
@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})
export class ListPage {
  @ViewChild(Content) content: Content;
  type;
  id;
  list;
  title;
  dataRes;
  number;
  typeId;
  items: any = [];
  offset = 0;
  length = 10;
  top = false;

  // start: any = {};
  // end: any = {};
  start = Utils.dateFormat(new Date(), 'yyyy-MM-ddTHH:mm:ss+08:00');
  end = Utils.dateFormat(new Date(), 'yyyy-MM-ddTHH:mm:ss+08:00');
  text;
  rowSelectionTable;
  constructor(
    private cd: ChangeDetectorRef,
    public service1 :DetailsService,
    public navCtrl: NavController,
    public navParams: NavParams,
    public service: ListService) {
    this.type = this.navParams.get('type')
    console.log(this.type);
    this.id = this.navParams.get('id')
    this.title = this.navParams.get('title')
    this.number = this.navParams.get('typeNumber')
    this.typeId = this.navParams.get('type');
    console.log(this.typeId);
  }

  ionViewDidLoad() {

    //获取单位
    this.service1.getname(this.number)
    .subscribe(res => {
      // this.dataName = res;
      this.dataRes = res;
      console.log(777,this.dataRes);
      console.log(this.dataRes[0].name);
    })

  }

  ionViewWillLeave() {
  }

  search1() {

        const start = typeof this.start, end = typeof this.end;
    
        console.log(start);
        console.log(end);
    
        if (start == 'object' && end == 'object') {
          this.text = "请选择开始时间与结束时间"
        } else if (start == 'object') {
          this.text = "请选择开始时间"
        } else if (end == "object") {
          this.text = "请选择结束时间"
        } else if (start == 'string' && end == 'string') {
          this.start = moment(this.start, "YYYY-MM-DD't'HH:mm:ss'Z'").format('YYYYMMDDHHmmssSSS');
          this.end = moment(this.end, "YYYY-MM-DD't'HH:mm:ss'Z'").format('YYYYMMDDHHmmssSSS');
          console.log(1, this.start);
          console.log(2, this.end);
          if (this.start < this.end) {
      
            // console.log(1111, parseInt(this.start) - (3600 * 20 * 1000));
            // console.log(22222, parseInt(this.end) - (3600 * 20 * 1000));
            this.items = [];
            this.offset = 0;
    
            this.queryAllData(this.start, this.end, '')
        
            this.text = undefined;
            setTimeout(() => {
              $(".ant-pagination-jump-next").children("a").text("···")
            }, 100)
          } else {
            this.text = "请设置正确的时间";
          }
        }
      }
    
    
      queryAllData(start, end, infiniteScroll) {
        var that = this;
        this.service.queryAllData(this.typeId, this.id, start, end, that.offset, that.length)
          .subscribe(data => {
            console.log('queryAll:', data);
    
            // this.status = status;
            // this.time = moment(data['time'], "YYYYMMDDHHmmssSSS").format("MM/DD HH:mm");
            that.items = this.items.concat(data);   // 原始+后来的
            this.offset += 10
            if (infiniteScroll) {
              infiniteScroll.complete();   /*请求完成数据更新页面*/
              if (data.length < 10) {   /*没有数据 禁用上拉更新*/
                infiniteScroll.enable(false);
              }
            }
          }, err => console.error(err));
    
        }



  //下拉刷新
  doInfinite(infiniteScroll) {
    console.log('Begin async operation');
    this.queryAllData(parseInt(this.start), parseInt(this.end), infiniteScroll);
  }

  click() {
    this.text = undefined;
  }


  //回到最顶部
  scrollTo() {
    this.content.scrollToTop();
  }

  // 判断回到顶部按钮
  check() {
    // console.log(1111);
    if (this.content.scrollTop > 200) {
      this.top = true;
      // console.log(999, this.top);
    } else {
      this.top = false;
    }
    this.cd.detectChanges();

  }
 


  }

  









  




